# #!/usr/bin/env bash

# SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# cd "$SCRIPT_DIR" || exit

# # Add agent to PYTHONPATH so metagpt module can be found
# export PYTHONPATH="$SCRIPT_DIR/agent:$PYTHONPATH"

# # Add chatpilot to PYTHONPATH so chatpilot module can be found
# export PYTHONPATH="$SCRIPT_DIR/chatpilot:$PYTHONPATH"

# KEY_FILE=.webui_secret_key

# PORT="${PORT:-1280}"
# if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then
#   echo No WEBUI_SECRET_KEY provided

#   if ! [ -e "$KEY_FILE" ]; then
#     echo Generating WEBUI_SECRET_KEY
#     # Generate a random value to use as a WEBUI_SECRET_KEY in case the user didn't provide one.
#     echo $(head -c 12 /dev/random | base64) > $KEY_FILE
#   fi

#   echo Loading WEBUI_SECRET_KEY from $KEY_FILE
#   WEBUI_SECRET_KEY=`cat $KEY_FILE`
# fi

# export WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY"
# ps -ef | grep "chatpilot" | grep -v "grep" | awk '{print $2}' | xargs kill -9
# # uvicorn chatpilot.server:app --host 0.0.0.0 --port $PORT --proxy-headers --workers 2
# gunicorn -k uvicorn.workers.UvicornWorker chatpilot.server:app --bind 0.0.0.0:$PORT --forwarded-allow-ips '*' -w 2

#!/usr/bin/env bash

# --- 1. 准备工作：进入脚本所在目录 ---
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR" || exit

# --- 2. 检查核心依赖 uv 是否存在 ---
if ! command -v uv &> /dev/null; then
    echo "Error: 'uv' command not found."
    echo "Please install uv first by running this command in your WSL terminal:"
    echo "curl -LsSf https://astral.sh/uv/install.sh | sh"
    exit 1
fi

# --- 3. 使用 uv 检查并创建虚拟环境 ---
if [ ! -d ".venv" ]; then
  echo "Virtual environment '.venv' not found. Creating it now with 'uv venv'..."
  # 使用 uv 创建 venv
  uv venv
  if [ $? -ne 0 ]; then
    echo "Error: Failed to create virtual environment with uv."
    exit 1
  fi
fi

# --- 4. 激活虚拟环境 ---
echo "Activating virtual environment..."
source .venv/bin/activate
if [ $? -ne 0 ]; then
  echo "Error: Failed to activate virtual environment."
  exit 1
fi

# --- 5. 使用 uv 安装/更新依赖 ---
echo "Installing/updating dependencies with 'uv pip'..."
# a. 从 requirements.txt 安装
if [ -f "requirements.txt" ]; then
  uv pip install -r requirements.txt
else
  echo "Warning: requirements.txt not found."
fi
# b. 确保 metagpt 被安装
uv pip install metagpt
if [ $? -ne 0 ]; then
  echo "Error: Failed to install Python dependencies with uv."
  exit 1
fi

# --- 6. 设置应用环境变量 ---
export PYTHONPATH="$SCRIPT_DIR/agent:$PYTHONPATH"
export PYTHONPATH="$SCRIPT_DIR/chatpilot:$PYTHONPATH"

KEY_FILE=.webui_secret_key
PORT="${PORT:-1280}"
if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then
  echo No WEBUI_SECRET_KEY provided

  if ! [ -e "$KEY_FILE" ]; then
    echo Generating WEBUI_SECRET_KEY
    echo $(head -c 12 /dev/random | base64) > $KEY_FILE
  fi

  echo Loading WEBUI_SECRET_KEY from $KEY_FILE
  WEBUI_SECRET_KEY=`cat $KEY_FILE`
fi
export WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY"


# --- 7. 运行应用 ---
echo "Stopping old processes and starting the application..."
# 清理旧进程
ps -ef | grep "chatpilot" | grep -v "grep" | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1

# 启动 Gunicorn 服务
gunicorn -k uvicorn.workers.UvicornWorker chatpilot.server:app --bind 0.0.0.0:$PORT --forwarded-allow-ips '*' -w 2